package com.qili.controller.sys;

import com.qili.base.entity.CurrentUser;
import com.qili.core.annotation.Log;
import com.qili.entity.base.BaseCompanyOwner;
import com.qili.service.base.BaseCompanyOwnerService;
import com.qili.util.DateUtil;
import com.qili.util.JsonUtil;
import com.qili.util.ReType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author liuyang
 * @date 2021/1/8
 * @description
 */
@Slf4j
@Controller
@RequestMapping("/CompanyOwner")
public class CompanyOwnerController {

    @Autowired
    private BaseCompanyOwnerService baseCompanyOwnerService;

    @RequestMapping("/index")
    public String index(){
        return "/system/CompanyOwner/CompanyOwnerList";
    }

    @ApiOperation(value = "/getList", httpMethod = "GET", notes = "展示监理单位")
    @GetMapping(value = "/getList")
    @ResponseBody
    @RequiresPermissions("Company:show")
    public ReType showRoleList(BaseCompanyOwner CompanyOwner, Model model, String page, String limit) {
        return baseCompanyOwnerService.show(CompanyOwner, Integer.valueOf(page), Integer.valueOf(limit));
    }

    @GetMapping(value = "showAddCompanyOwner")
    public String goAddRole(Model model) {
        return "/system/CompanyOwner/addCompanyOwner";
    }

    @ApiOperation(value = "/addCompanyOwner", httpMethod = "POST", notes = "添加监理单位")
    @Log(desc = "添加监理单位")
    @PostMapping(value = "/addCompanyOwner")
    @ResponseBody
    public JsonUtil addCompanyOwner(BaseCompanyOwner CompanyOwner) {
        if (StringUtils.isBlank(CompanyOwner.getBcName())) {
            JsonUtil.error("单位名称不能为空");
        }

        BaseCompanyOwner sameNameCompanyOwner = new BaseCompanyOwner();
        sameNameCompanyOwner.setBcName(CompanyOwner.getBcName());
        List<BaseCompanyOwner> select = baseCompanyOwnerService.select(sameNameCompanyOwner);
        if(select.size() != 0){
            return JsonUtil.error("已经存在的单位名称");
        }

        CompanyOwner.setBcCreateTime(DateUtil.getCurrentDateTimeString());
        Subject sub = SecurityUtils.getSubject();
        CurrentUser currentUser = (CurrentUser) sub.getPrincipal();
        CompanyOwner.setBcCreateby(currentUser.getId());
        baseCompanyOwnerService.insertSelective(CompanyOwner);
        return JsonUtil.sucess("操作成功");
    }

    @GetMapping(value = "updateCompanyOwner")
    public String updateCompanyOwner(String bcId, Model model, boolean detail) {
        if (StringUtils.isNotEmpty(bcId)) {
            BaseCompanyOwner CompanyOwner = baseCompanyOwnerService.selectByPrimaryKey(bcId);
            model.addAttribute("CompanyOwner", CompanyOwner);
        }
        model.addAttribute("detail", detail);
        return "/system/CompanyOwner/updateCompanyOwner";
    }

    @ApiOperation(value = "/updateCompanyOwner", httpMethod = "POST", notes = "更新监理单位")
    @Log(desc = "更新监理单位")
    @PostMapping(value = "updateCompanyOwner")
    @ResponseBody
    public JsonUtil updateCompanyOwner(BaseCompanyOwner baseCompanyOwner) {
        if (baseCompanyOwner == null) {
            return JsonUtil.error("获取数据失败");
        }

        BaseCompanyOwner oldBaseCompanyOwner = baseCompanyOwnerService.selectByPrimaryKey(baseCompanyOwner);
        if(!oldBaseCompanyOwner.getBcName().equals(baseCompanyOwner.getBcName())){
            //修改了名称,验证新名称是否有重复:
            BaseCompanyOwner sameNameCompanyOwner = new BaseCompanyOwner();
            sameNameCompanyOwner.setBcName(baseCompanyOwner.getBcName());
            List<BaseCompanyOwner> select = baseCompanyOwnerService.select(sameNameCompanyOwner);
            if(select.size() > 0){
                return JsonUtil.error("已经存在的监理单位名称");
            }
        }

        baseCompanyOwnerService.updateByPrimaryKeySelective(baseCompanyOwner);
        return JsonUtil.sucess("操作成功");
    }

    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除监理单位")
    @Log(desc = "删除监理单位")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions("Company:del")
    public JsonUtil del(String bcId) {
        if (StringUtils.isEmpty(bcId)) {
            return JsonUtil.error("获取数据失败");
        }
        baseCompanyOwnerService.deleteByPrimaryKey(bcId);
        return JsonUtil.sucess("操作成功");
    }


}
